/**
 * 1.本代码为云笔记web端开发小组共同拥有。
 * 2.组内成员有 聂道鑫、徐斌强、何瑞、钟倩。
 * 3.联系方式：442342707@qq.com
 */
package com.yunbiji.controller;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yunbiji.interceptor.LoginInterceptor;
import com.yunbiji.interceptor.MyCaptchaRenderInterceptor;
import com.yunbiji.model.Users;
import com.yunbiji.render.MyCaptchaRender;
import com.yunbiji.validate.LoginValidate;
/**
 * 登陆验证类
 * @author 聂道鑫
 *
 */
public class LoginController extends Controller
{
	
	private static final Logger logger = Logger.getLogger(LoginController.class);
	
	/**
	 * 返回登陆界面
	 */
	public void index()
	{

		render("login.html");
	}

	/**
	 * nie:登录验证
	 */
	@Before({LoginValidate.class,MyCaptchaRenderInterceptor.class,LoginInterceptor.class})//登录验证模块
	public void login()
	{
		logger.info("login方法开始执行");
		String username = getPara("username");
		setSessionAttr("username",username);
		String passwd = getPara("passwd");
		//String remember = getPara("remember");

		try
		{
			boolean success = Users.dao.login(username, passwd);
			if (!success)
			{
				keepPara("username");
				setAttr("title", "登录");
				setAttr("error", "密码错误,请重新输入密码！");
				render("login.html");
			}
			else
			{

				setSessionAttr("username", username);// nie: 将用户名保存在session中
				setAttr("title", "主页");
				render("welcome.html");
			}
			logger.info("login方法执行结束");
		}
		catch (Exception e)
		{
			{
				logger.error("login方法程序出现异常");
				setAttr("alertInfo", "登录出错！请重新登录.2秒后自动返回登录界面！");
				render("errors/alert.html");
				getResponse().setHeader("Refresh", "2;URL=index");
			}
		}

	}

	/**
	 * 处理注销事件
	 */
	public void exit()
	{
		logger.info("exit方法开始执行");
		getRequest().getSession().invalidate();
		setAttr("alertInfo", "成功退出，两秒后返回登录界面！");
		render("errors/alert.html");
		getResponse().setHeader("Refresh", "2;URL=index");
		logger.info("exit结束执行");
	}
	/**
	 * 注册按钮的跳转
	 */
	public void registerInLoign()
	{
		render("register.html");
	}
	
	/**
	 *生成验证码 
	 */
	public void captcha()
    {
        render(new MyCaptchaRender(60,22,4,true));
    }
	
	/**
	 * 提供给钟倩调试用的接口客户页
	 */
	public void customer()
	{
		render("customer.html");
	}

}
